﻿using OpenAuth.Repository.Domain;
using System;
using System.Collections.Generic;
using System.Text;
using OpenAuth.App.Interface;
using OpenAuth.Repository.Interface;
using OpenAuth.App.Request;
using Infrastructure;

namespace OpenAuth.App
{
    public class PayOrderZYApp : BaseApp<PayOrderZY>
    {
        private RevelanceManagerApp _revelanceApp;
        private IAuth _auth;
        public PayOrderZYApp(IUnitWork unitWork, IRepository<PayOrderZY> repository,
            RevelanceManagerApp app, IAuth auth) : base(unitWork, repository)
        {
            _revelanceApp = app;
            _auth = auth;
        } 
        
        /// <summary>
        /// 通过订单号获取充值记录
        /// </summary>
        /// <param name="orderId">订单号</param>
        /// <param name="patid">患者id</param>
        /// <returns></returns>
        public AddOrUpdatePayOrderZYReq GetByOrder(string orderId, string patid)
        {
            return Repository.FindSingle(x => x.patid == patid && x.OrderNumber == orderId).MapTo<AddOrUpdatePayOrderZYReq>();
        } 

        /// <summary>
        /// 通过订单号获取zfzt是1的记录（因为银行返回多次报文）
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="patid"></param>
        /// <returns></returns>
        public AddOrUpdatePayOrderZYReq GetByOrderBystate(string orderId, string patid)
        {
            return Repository.FindSingle(x => x.patid == patid && x.OrderNumber == orderId && x.zfzt == 1).MapTo<AddOrUpdatePayOrderZYReq>();
        } 
        
        /// <summary>
        /// 通过支付状态获取充值记录
        /// </summary>
        /// <param name="zfzt">充值状态</param>
        /// <returns></returns>
        public List<AddOrUpdatePayOrderZYReq> GetListByzfzt(int zfzt)
        {
            //&& x.OrderNumber == "202002061413386410789596171"
            var query = Repository.Find(x => x.zfzt == zfzt );
            var items = query.MapToList<AddOrUpdatePayOrderZYReq>();
            return items;
        }  
        
        /// <summary>
        /// 根据patid获取一个月内的充值List
        /// </summary>
        /// <param name="patid">患者id</param>
        /// <param name="stime">开始时间</param>
        /// <param name="etime">结束时间</param>
        /// <returns></returns>
        public List<AddOrUpdatePayOrderZYReq> GetListByPatid(string patid, DateTime stime, DateTime etime)
        {

            var query = Repository.Find(x => x.zfzt == 2 && x.patid == patid && x.time <= etime && x.time >= stime);
            var item = query.MapToList<AddOrUpdatePayOrderZYReq>();
            return item;
        }   

        /// <summary>
        /// 查询充值记录状态
        /// </summary>
        /// <param name="ordernumber">订单号</param>
        /// <param name="patid">患者id</param>
        /// <returns></returns>
        public bool GetchargeState(string ordernumber, string patid)
        {
            var query = Repository.FindSingle(x => x.zfzt == 2 && x.patid == patid && x.OrderNumber == ordernumber);
            var item = query.MapTo<AddOrUpdatePayOrderZYReq>();
            if (item != null)
            {
                return true;
            }
            else
            {
                return false;
            }

        }    
        
        /// <summary>
        /// 更新方法
        /// </summary>
        /// <param name="obj"></param>
        public void Update(AddOrUpdatePayOrderZYReq obj)
        {

            Repository.Update(u => u.Id == obj.Id, u => new PayOrderZY
            {
                OrderNumber = obj.OrderNumber,
                bxh = obj.bxh,
                hzxm = obj.hzxm,
                zfje = obj.zfje,
                zfzt = obj.zfzt,
                time = obj.time,
                zfPhone = obj.zfPhone,
                patid = obj.patid,
                hisdh = obj.hisdh,
                sjh = obj.sjh,
                yjxh = obj.yjxh,

                blh = obj.blh,
                jzlsh = obj.jzlsh,
                yjlsh = obj.yjlsh,
                hisddh = obj.hisddh

                //UpdateTime = DateTime.Now,
                //UpdateUserId = user.Id,
                //UpdateUserName = user.Name
                //todo:补充或调整自己需要的字段
            });

        }

        public void Add(AddOrUpdatePayOrderZYReq req)
        {
            var obj = req.MapTo<PayOrderZY>();
            //todo:补充或调整自己需要的字段
            //obj.CreateTime = DateTime.Now;
            //var user = _auth.GetCurrentUser().User;
            //obj.CreateUserId = user.Id;
            //obj.CreateUserName = user.Name;
            Repository.Add(obj);
        }
    }
}
